<template>
  <el-dialog
    :model-value="show"
    title="驳回申请原因填写"
    width="800px"
    append-to-body
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    class="form-label-120"
    destroy-on-close
  >
    <el-form
      ref="rejectFormRef"
      :model="rejectForm"
      :rules="rules"
      label-width="80px"
      class="form-box"
    >
      <el-form-item label="驳回原因：" prop="reason" style="width: 100%">
        <el-input
          type="textarea"
          resize="none"
          :rows="5"
          maxlength="300"
          show-word-limit
          v-model="rejectForm.reason"
          placeholder="请填写驳回原因，至少5字"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        :disabled="!show"
        @click="submit"
      >
        确认驳回
      </el-button>
      <el-button type="info" @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { platformApplicationReject } from '@/api/resources';
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  activeRow: {
    type: Object,
    default: () => {}
  }
});
const emit = defineEmits();

watch(
  () => props.show,
  val => {
    if (!val) return;
    rejectForm.value = {};
  }
);

// 关闭
function handleClose() {
  emit('update:show', false);
}

const rejectForm = ref({});
const rules = reactive({
  reason: [
    {
      required: true,
      message: '请输入驳回原因',
      trigger: 'change'
    },
    {
      min: 5,
      message: '驳回原因至少5字',
      trigger: 'change'
    }
  ]
});

// 确认驳回
const loading = ref(false);
const rejectFormRef = ref();
async function submit() {
  try {
    await rejectFormRef.value.validate();
    loading.value = true;
    platformApplicationReject({
      id: props.activeRow.id,
      remark: rejectForm.value.reason
    })
      .then(() => {
        handleClose();
        emit('submit');
      })
      .finally(() => {
        loading.value = false;
      });
  } catch {
    console.log('申请处理-新增站点资源-表单提交校验未通过');
  }
}
</script>

<style lang="scss" scoped></style>
